package com.slt.developkitweb.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import java.util.function.Function;
import java.util.function.Predicate;

/***
 * @author shalongteng
 * @date 2023年11月24日 10:05
 **/
public class test {
    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 40, 60L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(5000));

    //定义一个Predicate函数
    private static <T> Predicate<T> myPredicate(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
    //线程开启的太快，往阻塞队列里放的太快，线程处理不过来，队列5000差不多5200个任务就抛异常了。
    public static void main(String[] args) throws InterruptedException {
        int sum = 1;

        for (int i = 0; i < 20000; i++) {
//            Thread.sleep(1);
            int finalI = i;
            sum++;
            int finalSum = sum;
            threadPool.submit(() -> {
                System.out.println(finalSum + " 线程名称为，" + Thread.currentThread().getName());
            });
        }
    }

    public static Set getSet() {
        Set set = new HashSet();
        set.add("hello");
        set.add("slt");
        set.add("haha");
        return set;
    }

    public static List getList() {
        List list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        return list;
    }

    public static List getList2() {
        List list = new ArrayList<>();
        list.add("3");
        list.add("4");
        list.add("5");
        return list;
    }
}
